﻿using Domain.Entity;
using DtoFw.Enum;
using Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Service.Base
{
    public class BaseService
    {
        private readonly MainSerialNumberRepository _mainSerialNumberRepository;
        public BaseService(MainSerialNumberRepository mainSerialNumberRepository)
        {
            this._mainSerialNumberRepository = mainSerialNumberRepository;
        }
        /// <summary>
        /// 获取编号类型
        /// </summary>
        /// <param name="AreCode"></param>
        /// <param name="NumberTypeCode"></param>
        /// <returns></returns>
        public string GetNumber(string AreCode,
            long NumberId)
        {
            var entity = _mainSerialNumberRepository.Query(new DtoFw.BaseQuery(),
                 t => t.Where(m => m.AreaCode == AreCode && m.NumberId == NumberId))?.First();
            return "";
        }
        /// <summary>
        /// DB对象
        /// </summary>
        /// <returns></returns>
        public SqlSugarClient dbClient()
        {
            return _mainSerialNumberRepository.GetDb();
        }
        /// <summary>
        /// 获取编号类型
        /// </summary>
        /// <param name="AreCode"></param>
        /// <param name="NumberTypeCode"></param>
        /// <returns></returns>
        public string GetNumber(long organId, EnumSystemNumber numberCode)
        {
            MainNumberEntity entity = _mainSerialNumberRepository.GetNumberEntity(organId, numberCode);
            if (entity == null)
                return "";
            int inc = (Convert.ToInt32(entity.Inc) + 1);
            if (inc.ToString().Length > entity.Inc.Length)
                return "";
            entity.Inc = inc.ToString().PadLeft(entity.Inc.Length, '0');
            entity.CurrentNumber = entity.NumberFormat
                 .Replace("区划编号", entity.AreaCode).Replace("时间标识",
                     DateTime.Now.ToString(entity.DateForamt)).Replace("自增长度", entity.Inc);
            _mainSerialNumberRepository.Save(entity);
            return entity.CurrentNumber;
        }
    }
}
